package com.vito.dcs.process.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.vito.base.factory.IDFactory;
import com.vito.base.model.DataGrid;
import com.vito.dcs.process.dao.PavingProcessMapper;
import com.vito.dcs.process.model.CompationProcessModel;
import com.vito.dcs.process.model.PavingProcessModel;
import com.vito.dcs.process.service.PavingProcessService;

@Service
public class PavingProcessServiceImpl implements PavingProcessService{

	@Autowired
	private PavingProcessMapper pavingProcessMapper;
	
	
	@Override
	public void savePavingProcessModel(PavingProcessModel processModel) {
		
		String pavingId = IDFactory.getIDFactory().getIdInc("tbl_dcs_sys_paving_process")+"";
		
		processModel.setPavingId(pavingId);
		processModel.setProcessVersion(pavingId);
		
		processData(processModel, pavingId);
		
		pavingProcessMapper.savePavingProcessModel(processModel);
		pavingProcessMapper.saveCompationProcessModel(processModel);
		
	}

	private void processData(PavingProcessModel processModel, String pavingId) {
		String processJson = processModel.getProcessJson();
		@SuppressWarnings("unchecked")
		Map<String,String> map = (Map<String, String>) JSONObject.parse(processJson);
		
		List<CompationProcessModel> list = new ArrayList<CompationProcessModel>();
		
		String[] str = new String[]{"init","rep","final"};
		for(int i = 0; i < str.length; i ++){
			
			CompationProcessModel compationProcessModel = new CompationProcessModel();
			
			String comId = IDFactory.getIDFactory().getIdInc("tbl_dcs_sys_compation_process")+"";
			compationProcessModel.setId(comId);
			compationProcessModel.setPavingId(pavingId);
			
			String processType = "";
			switch (str[i]) {
			case "init":
				processType = "01";
				break;
			case "rep":
				processType = "02";
				break;
			case "final":
				processType = "03";
				break;
			default:
				break;
			}
			
			compationProcessModel.setProcessType(processType);
			
			compationProcessModel.setRollerType(map.get(str[i]+"_roller_type"));
			compationProcessModel.setStatus(map.get(str[i]+"_status"));
			compationProcessModel.setMinCompation(map.get(str[i]+"_min_compation"));
			compationProcessModel.setMaxCompation(map.get(str[i]+"_max_compation"));
			compationProcessModel.setMinSpeed(map.get(str[i]+"_min_speed"));
			compationProcessModel.setMaxSpeed(map.get(str[i]+"_max_speed"));
			
			/**
			 * 沥青工艺--->压实工艺才有
			 */
			compationProcessModel.setMinTemperature(map.get(str[i]+"_min_temperature"));
			compationProcessModel.setMaxTemperature(map.get(str[i]+"_max_temperature"));
			
			list.add(compationProcessModel);
			
		}
		
		processModel.setListCompation(list);
	}
	
	@Override
	public DataGrid<PavingProcessModel> queryPavingProcessByPage(
			PavingProcessModel processModel) {
		DataGrid<PavingProcessModel> data = new DataGrid<PavingProcessModel>();
		List<PavingProcessModel> queryPavingProcessByPage = pavingProcessMapper.queryPavingProcessByPage(processModel);
		data.setRows(queryPavingProcessByPage);
		data.setTotal(processModel.getTotal());
		return data;
	}

	@Override
	public void updatePavingProcessModel(PavingProcessModel processModel) {
		
		String pavingId = processModel.getPavingId();
		processData(processModel, pavingId );
		pavingProcessMapper.updatePavingProcess(processModel);
		pavingProcessMapper.deleteCompationProcess(processModel);
		pavingProcessMapper.saveCompationProcessModel(processModel);
		
	}

	@Override
	public void deletePavingProcess(PavingProcessModel processModel) {
		if(processModel.getCheckeds().length > 0){
			pavingProcessMapper.deletePavingProcess(processModel);
		}
	}

}
